package com.mashibing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mashibing.internalcommon.constant.CommonStatusEnum;
import com.mashibing.internalcommon.constant.DriverCarConstants;
import com.mashibing.internalcommon.dto.DriverCarBindingRelationship;
import com.mashibing.internalcommon.dto.DriverUser;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.mapper.DriverCarBindingRelationshipMapper;
import com.mashibing.mapper.DriverUserMapper;
import com.mashibing.service.IDriverCarBindingRelationshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 孟瑞达
 * @since 2025-06-18
 */
@Service
public class DriverCarBindingRelationshipServiceImpl implements IDriverCarBindingRelationshipService {

    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;

    @Autowired
    private DriverUserMapper driverUserMapper;


    @Override
    public ResponseResult bind(DriverCarBindingRelationship driverCarBindingRelationship) {

        // 司机与车辆已经绑定了
        QueryWrapper<DriverCarBindingRelationship> wrapper = new QueryWrapper<>();
        wrapper.eq("driver_id", driverCarBindingRelationship.getDriverId());
        wrapper.eq("car_id", driverCarBindingRelationship.getCarId());
        wrapper.eq("bind_state", DriverCarConstants.DRIVER_CAR_BIND);
        Integer count = driverCarBindingRelationshipMapper.selectCount(wrapper);
        if (count > 0) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getCode(), CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getMessage());
        }

        // 司机被绑定了
        wrapper = new QueryWrapper<>();
        wrapper.eq("driver_id", driverCarBindingRelationship.getDriverId());
        wrapper.eq("bind_state", DriverCarConstants.DRIVER_CAR_BIND);
        count = driverCarBindingRelationshipMapper.selectCount(wrapper);
        if ((count > 0)) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_BIND_EXISTS.getCode(), CommonStatusEnum.DRIVER_BIND_EXISTS.getMessage());
        }

        // 车辆被绑定了
        wrapper = new QueryWrapper<>();
        wrapper.eq("car_id", driverCarBindingRelationship.getCarId());
        wrapper.eq("bind_state", DriverCarConstants.DRIVER_CAR_BIND);
        count = driverCarBindingRelationshipMapper.selectCount(wrapper);
        if ((count > 0)) {
            return ResponseResult.fail(CommonStatusEnum.CAR_BIND_EXISTS.getCode(), CommonStatusEnum.CAR_BIND_EXISTS.getMessage());
        }

        // 插入数据库
        DriverCarBindingRelationship relationship = new DriverCarBindingRelationship();
        relationship.setDriverId(driverCarBindingRelationship.getDriverId());
        relationship.setCarId(driverCarBindingRelationship.getCarId());
        relationship.setBindingTime(LocalDateTime.now());
        relationship.setBindState(DriverCarConstants.DRIVER_CAR_BIND);
        driverCarBindingRelationshipMapper.insert(relationship);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult unBind(DriverCarBindingRelationship driverCarBindingRelationship) {

        // 查询绑定关系
        QueryWrapper<DriverCarBindingRelationship> wrapper = new QueryWrapper<>();
        wrapper.eq("driver_id", driverCarBindingRelationship.getDriverId());
        wrapper.eq("car_id", driverCarBindingRelationship.getCarId());
        wrapper.eq("bind_state", DriverCarConstants.DRIVER_CAR_BIND);
        DriverCarBindingRelationship relationship = driverCarBindingRelationshipMapper.selectOne(wrapper);
        if (relationship == null) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(), CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getMessage());
        }

        // 解绑存库
        relationship.setBindState(DriverCarConstants.DRIVER_CAR_UNBIND);
        relationship.setUnBindingTime(LocalDateTime.now());
        driverCarBindingRelationshipMapper.updateById(relationship);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult<DriverCarBindingRelationship> getDriverCarRelationShipByDriverPhone(String driverPhone) {
        QueryWrapper<DriverUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_phone", driverPhone);

        DriverUser driverUser = driverUserMapper.selectOne(queryWrapper);
        Long driverId = driverUser.getId();

        QueryWrapper<DriverCarBindingRelationship> driverCarBindingRelationshipQueryWrapper = new QueryWrapper<>();
        driverCarBindingRelationshipQueryWrapper.eq("driver_id", driverId);
        driverCarBindingRelationshipQueryWrapper.eq("bind_state", DriverCarConstants.DRIVER_CAR_BIND);

        DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingRelationshipMapper.selectOne(driverCarBindingRelationshipQueryWrapper);
        return ResponseResult.success(driverCarBindingRelationship);

    }


}
